package org.jadestudio.home.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jadestudio.home.dao.CommonDao;
import org.jadestudio.home.service.GameService;
import org.jadestudio.home.service.LeaderboardService;

public class GameDetailAction extends Action {
	private GameService gameService;
	
	public GameService getGameService() {
		return gameService;
	}
	public void setGameService(GameService gameService) {
		this.gameService = gameService;
	}
	
	/**
	 * 加载数据
	 */
	@SuppressWarnings("unchecked")
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception{
		
		String id = request.getParameter("id");
		if (id==null) id="";
		
		String ip = CommonDao.getIP(request);
		
		// 加载游戏详情
		java.util.Map gameDetail = gameService.getGameDetail(id);
		request.setAttribute("gameDetail", gameDetail);
		
		// 加载平台对应名称
		if (gameDetail.get("game_platform_id")!=null){
			String game_platform_ids = gameDetail.get("game_platform_id").toString();
			List<java.util.Map> gamePlatforms = gameService.getGamePlatformById(game_platform_ids);
			request.setAttribute("gamePlatforms", gamePlatforms);
		}
		
		// 加载截图
		List<java.util.Map> gameScreenshots = gameService.getGameScreenshotList(id);
		request.setAttribute("gameScreenshots", gameScreenshots);

		// 加载类似游戏
		List<java.util.Map> gameSimilars = gameService.getSimilarGameList(id, gameDetail.get("game_tag")==null?"":gameDetail.get("game_tag").toString());
		request.setAttribute("gameSimilars", gameSimilars);
		
		// 加载游戏评分
		java.util.Map gameVoteList = gameService.getGameVotes(id,ip);
		request.setAttribute("gameVoteList", gameVoteList);
		
		// 加载游戏相关文章列表
		List<java.util.Map> articleList = gameService.getGameArticleList(id);
		request.setAttribute("articleList", articleList);
		
		// 加载同厂商游戏
		List<java.util.Map> gameList = gameService.getGamesListByCompany(id, gameDetail.get("dev_company")==null?"":gameDetail.get("dev_company").toString(), gameDetail.get("publish_company_id")==null?"":gameDetail.get("publish_company_id").toString());
		request.setAttribute("gameList", gameList);
		
		return mapping.findForward("success");
	}
}
